﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmManagePlatform
{
    public partial class Demo2 : RelationsLMP.Control.Form.MetroDemoForms
    {

        List<Model.DB.tb_ParaInfo> _List, _TempList;
        public Demo2()
        {
            InitializeComponent();

            InitControls();

            _List = new List<Model.DB.tb_ParaInfo>()
            {
                new Model.DB.tb_ParaInfo(){Id = 41,Code = "O2",Desc ="气体测试类型O2" },
                new Model.DB.tb_ParaInfo(){Id = 47,Code = "H2S",Desc ="气体H2S" },
                new Model.DB.tb_ParaInfo(){Id = 49,Code = "CO",Desc ="CO氧气与氯气" },
                new Model.DB.tb_ParaInfo(){Id = 50,Code = "CH4",Desc ="CH4碳元素" },
                new Model.DB.tb_ParaInfo(){Id = 51,Code = "CH4",Desc ="CO氧气与氯气" },
                new Model.DB.tb_ParaInfo(){Id = 52,Code = "CH4",Desc ="CH4碳元素" },
                new Model.DB.tb_ParaInfo(){Id = 53,Code = "CH4",Desc ="气体H2S" },
                new Model.DB.tb_ParaInfo(){Id = 54,Code = "CH4",Desc ="CH4碳元素" },
            };


            List<string> _ListString = _List.Select(x=>x.Desc).ToList();

            _ListString = _ListString.Distinct().ToList();
            _List.Insert(0, new Model.DB.tb_ParaInfo() { Id = -1, Code = "null", Desc = "气体类型" });

            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
            comboBox1.DisplayMember = "Desc";
            comboBox1.ValueMember = "Id";
            comboBox1.Items.AddRange(_List.ToArray());



            comboBox1.TextUpdate += ComboBox1_TextChanged;
            comboBox1.DropDownClosed += ComboBox1_DropDownClosed;

            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }
        }

        UC.ComboBox.SubmitUnit Manufacturer;
        public void InitControls()
        {
            Manufacturer = new UC.ComboBox.SubmitUnit();
            Manufacturer.Dock = DockStyle.Fill;
            panel1.Controls.Add(Manufacturer);

            comBoxFilter1.DropDownStyle = ComboBoxStyle.DropDown;
            comBoxFilter1.addItems(new List<string>()
            {
                "西北测试1",
                "西北测试2",
                "西北测试3",
                "西北测试4",
                "西北测试5",
                "东北测试1",
                "东北测试2",
                "东北测试3",
                "东北测试4",
                "东北测试5",
                "西南测试1",
                "西南测试2",
                "西南测试3",
                "西南测试4",
                "西南测试5",
                "东南测试1",
                "东南测试2",
                "东南测试3",
                "东南测试4",
                "东南测试5",
            }.ToArray());
        }



        private void ComboBox1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (_List.Count <= 0)
                    return;

                for (int i = 0; i < _TempList.Count; i++)
                {
                    if (_TempList[i].Desc == comboBox1.Text)
                    {
                        comboBox1.SelectedIndex = i;
                        return;
                    }
                }

                if (_TempList.Count > 0)
                {
                    comboBox1.SelectedIndex = 0;
                }
                else
                {
                    comboBox1.Items.Clear();
                    comboBox1.Items.AddRange(_List.ToArray());
                    comboBox1.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                string s1 = string.Empty;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                string s1 = Manufacturer.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "获取查询条件失败，提示信息：" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Manufacturer.Focus();
                return;
            }

        }

        private void ComboBox1_TextChanged(object sender, EventArgs e)
        {

            try
            {
                if (comboBox1.DropDownStyle == ComboBoxStyle.DropDownList) return;
                string stringNew = comboBox1.Text;
                _TempList = new List<Model.DB.tb_ParaInfo>();
                comboBox1.Items.Clear();
                for (int i = 0; i < _List.Count; i++)
                {
                    if (string.IsNullOrWhiteSpace(stringNew) || _List[i].Desc.Contains(stringNew))
                        _TempList.Add(_List[i]);
                }

                if (_TempList.Count > 0)
                {
                    comboBox1.Items.AddRange(_TempList.ToArray());
                }
                this.Cursor = Cursors.Arrow;
                comboBox1.SelectionStart = comboBox1.Text.Length;
                comboBox1.DroppedDown = true;
            }
            catch (Exception ex)
            {
                string s1 = string.Empty;
            }
        }
    }
}
